import Vue from 'vue'
import Vuex from 'vuex'

import storage from "@/store/storage";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    username: ''
  },

  // const getters = {
  //   get_token(state){
  //     return state.token || storage.get("token") || null;
  //   }
  // },

  // 计算属性
  mutations: {
    get_token(state){
      return state.token || storage.get("token") || null;
    },
    // 修改token，并将token存入localStorage
    set_token(state,token) {
      state.token = token;
      storage.set('token', token);
      console.log('store、localstorage保存token成功！');
    },
    del_token(state) {
      state.token = "";
      storage.remove("token");
    },
    // 可选
    setUserInfo(state, userName) {
      state.userName = userName;
    }
  },

  actions: {
  },

  modules: {
  }
})
